<template>
  <el-dialog
    :title="!dataForm.newsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="楼盘名称" prop="newsName">
      <el-input v-model="dataForm.newsName" placeholder="楼盘名称"></el-input>
    </el-form-item>
    <el-form-item label="楼盘均价" prop="newsAvgPrice">
      <el-input v-model="dataForm.newsAvgPrice" placeholder="楼盘均价"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="newsAddr">
      <el-input v-model="dataForm.newsAddr" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="开盘时间" prop="newsStarttime">
      <el-input v-model="dataForm.newsStarttime" placeholder="开盘时间"></el-input>
    </el-form-item>
    <el-form-item label="销售状态 1在售 2停售" prop="newsStatus">
      <el-input v-model="dataForm.newsStatus" placeholder="销售状态 1在售 2停售"></el-input>
    </el-form-item>
    <el-form-item label="楼盘户型" prop="newsDoorMode">
      <el-input v-model="dataForm.newsDoorMode" placeholder="楼盘户型"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="newsRegionid">
      <el-input v-model="dataForm.newsRegionid" placeholder="地区id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          newsId: 0,
          newsName: '',
          newsAvgPrice: '',
          newsAddr: '',
          newsStarttime: '',
          newsStatus: '',
          newsDoorMode: '',
          newsRegionid: ''
        },
        dataRule: {
          newsName: [
            { required: true, message: '楼盘名称不能为空', trigger: 'blur' }
          ],
          newsAvgPrice: [
            { required: true, message: '楼盘均价不能为空', trigger: 'blur' }
          ],
          newsAddr: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          newsStarttime: [
            { required: true, message: '开盘时间不能为空', trigger: 'blur' }
          ],
          newsStatus: [
            { required: true, message: '销售状态 1在售 2停售不能为空', trigger: 'blur' }
          ],
          newsDoorMode: [
            { required: true, message: '楼盘户型不能为空', trigger: 'blur' }
          ],
          newsRegionid: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.newsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.newsId) {
            this.$http({
              url: this.$http.adornUrl(`/room/housenews/info/${this.dataForm.newsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.newsName = data.houseNews.newsName
                this.dataForm.newsAvgPrice = data.houseNews.newsAvgPrice
                this.dataForm.newsAddr = data.houseNews.newsAddr
                this.dataForm.newsStarttime = data.houseNews.newsStarttime
                this.dataForm.newsStatus = data.houseNews.newsStatus
                this.dataForm.newsDoorMode = data.houseNews.newsDoorMode
                this.dataForm.newsRegionid = data.houseNews.newsRegionid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/room/housenews/${!this.dataForm.newsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'newsId': this.dataForm.newsId || undefined,
                'newsName': this.dataForm.newsName,
                'newsAvgPrice': this.dataForm.newsAvgPrice,
                'newsAddr': this.dataForm.newsAddr,
                'newsStarttime': this.dataForm.newsStarttime,
                'newsStatus': this.dataForm.newsStatus,
                'newsDoorMode': this.dataForm.newsDoorMode,
                'newsRegionid': this.dataForm.newsRegionid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
